<template>
  <div class="register-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>企业人事管理系统 - 注册</span>
        </div>
      </template>
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="encryptedPassword">
          <el-input type="password" v-model="form.encryptedPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="form.confirmPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="login-link">
        <router-link to="/">已有账号？立即登录</router-link>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { useRouter } from 'vue-router';

const form = reactive({
  username: '',
  encryptedPassword: '',
  confirmPassword: '',
  phoneNumber: '',
  email:''
});
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== form.encryptedPassword) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  encryptedPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  phoneNumber: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
  ]
};

const ruleForm = ref(null);
const router = useRouter();
const submitForm = () => {
  ruleForm.value.validate((valid) => {
    if (valid) {
      // 发起注册请求
      axios.post('http://localhost:8080/users/register', {
        username: form.username,
        encryptedPassword: form.encryptedPassword,
        phoneNumber: form.phoneNumber,
        email:form.email
      }).then(response => {
        if (response.data.code==200) {
          ElMessage.success(response.data.message);
          // 跳转到登录页面
          router.push('/');
        } else {
          ElMessage.error(response.data.message);
        }
      }).catch(error => {
        console.error('注册失败:', error);
        ElMessage.error('服务器错误');
      });
    } else {
      console.log('表单验证失败!!');
      return false;
    }
  });
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.box-card {
  width: 480px;
}
.card-header {
  text-align: center;
}
.login-link {
  text-align: center;
  margin-top: 10px;
}
</style>